package com.evil.common.application.util;

import com.evil.common.application.constant.ApplicationRedisConstant;
import com.evil.common.core.constant.AppConstant;
import com.evil.common.core.util.RedisKeyUtil;
import com.evil.common.core.util.ServletUtils;
import com.evil.common.core.util.StreamUtil;

import java.util.Collection;
import java.util.Map;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
public class ApplicationRedisKeyUtil extends RedisKeyUtil {

    // region 应用包

    /**
     * 会话资源-应用包
     *
     * @return String
     */
    public static String requestUuidPackageKey(Long packageId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_PACKAGE_PREFIX + packageId;
    }

    /**
     * 会话资源-应用包
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidPackageKeyMap(Collection<Long> packageIds) {
        return StreamUtil.toMapKV(packageIds, e -> e, ApplicationRedisKeyUtil::requestUuidPackageKey);
    }
    // endregion

    // region 应用组

    /**
     * 会话资源-应用组
     *
     * @return String
     */
    public static String requestUuidGroupKey(Long groupId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_GROUP_PREFIX + groupId;
    }

    /**
     * 会话资源-应用组
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidGroupKeyMap(Collection<Long> groupIds) {
        return StreamUtil.toMapKV(groupIds, e -> e, ApplicationRedisKeyUtil::requestUuidGroupKey);
    }
    // endregion

    // region 应用

    /**
     * 会话资源-应用-ID版
     *
     * @return String
     */
    public static String requestUuidAppIdKey(Long appId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_APP_ID_PREFIX + appId;
    }

    /**
     * 会话资源-应用-ID版
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidAppIdKeyMap(Collection<Long> appIds) {
        return StreamUtil.toMapKV(appIds, e -> e, ApplicationRedisKeyUtil::requestUuidAppIdKey);
    }

    /**
     * 会话资源-应用-KEY版
     *
     * @return String
     */
    public static String requestUuidAppKeyKey(String key) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_APP_KEY_PREFIX + key;
    }

    /**
     * 会话资源-应用-KEY版
     *
     * @return Map
     */
    public static Map<String, String> requestUuidAppKeyKeyMap(Collection<String> keys) {
        return StreamUtil.toMapKV(keys, e -> e, ApplicationRedisKeyUtil::requestUuidAppKeyKey);
    }

    /**
     * 应用
     *
     * @param applicationId applicationId
     * @return String
     */
    public static String applicationIdKey(Long applicationId) {
        return ApplicationRedisConstant.APPLICATION_PREFIX + applicationId;
    }

    /**
     * 应用
     *
     * @param applicationIds applicationIds
     * @return String
     */
    public static Map<Long, String> applicationIdKey(Collection<Long> applicationIds) {
        return StreamUtil.toMapV(applicationIds, ApplicationRedisKeyUtil::applicationIdKey);
    }

    /**
     * 应用
     *
     * @param applicationKey applicationKey
     * @return String
     */
    public static String applicationKey(String applicationKey) {
        applicationKey = applicationKey.replaceAll("[#|:]", "__");
        return ApplicationRedisConstant.APPLICATION_KEY_PREFIX + applicationKey;
    }

    /**
     * 应用
     *
     * @param keys keys
     * @return String
     */
    public static Map<String, String> applicationKey(Collection<String> keys) {
        return StreamUtil.toMapV(keys, ApplicationRedisKeyUtil::applicationKey);
    }
    // endregion

    // region 表单

    /**
     * 会话资源-应用表单
     *
     * @return String
     */
    public static String requestUuidFormKey(Long formId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_FORM_PREFIX + formId;
    }

    /**
     * 会话资源-应用表单
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidFormKeyMap(Collection<Long> formIds) {
        return StreamUtil.toMapKV(formIds, e -> e, ApplicationRedisKeyUtil::requestUuidFormKey);
    }

    /**
     * 应用表单
     *
     * @param applicationId      applicationId
     * @param applicationVersion applicationVersion
     * @return String
     */
    public static String applicationFormKey(Long applicationId, Integer applicationVersion) {
        return ApplicationRedisConstant.REQUEST_UUID_FORM_PREFIX + applicationId + ":" + applicationVersion;
    }

    /**
     * 表单
     *
     * @param formId formId
     * @return String
     */
    public static String formKey(Long formId) {
        return ApplicationRedisConstant.FORM_PREFIX + formId;
    }

    /**
     * 表单
     *
     * @param formIds formIds
     * @return String
     */
    public static Map<Long, String> formKey(Collection<Long> formIds) {
        return StreamUtil.toMapV(formIds, ApplicationRedisKeyUtil::formKey);
    }
    // endregion

    // region 控件

    /**
     * 会话资源-控件
     *
     * @return String
     */
    public static String requestUuidControlKey(Long controlId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_CONTROL_PREFIX + controlId;
    }

    /**
     * 会话资源-控件
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidControlKeyMap(Collection<Long> controlIds) {
        return StreamUtil.toMapKV(controlIds, e -> e, ApplicationRedisKeyUtil::requestUuidControlKey);
    }

    /**
     * 企业应用表单控件树
     *
     * @param enterpriseId enterpriseId
     * @return String
     */
    public static String enterpriseAppTreeKey(Long enterpriseId, Integer applicationVersion) {
        return ApplicationRedisConstant.ENTERPRISE_APP_TREE_PREFIX + enterpriseId + ":" + applicationVersion;
    }

    /**
     * 企业应用表单控件
     *
     * @param enterpriseId enterpriseId
     * @return String
     */
    public static String enterpriseAppControlsKey(Long enterpriseId, Long applicationId, Integer applicationVersion) {
        return ApplicationRedisConstant.ENTERPRISE_APP_CONTROL_PREFIX + enterpriseId + ":" + applicationId + ":" + applicationVersion;
    }

    /**
     * 表单控件
     *
     * @param formId formId
     * @return String
     */
    public static String formControlsKey(Long formId) {
        return ApplicationRedisConstant.FORM_CONTROLS_PREFIX + formId;
    }

    /**
     * 表单控件
     *
     * @param formIds formIds
     * @return String
     */
    public static Map<Long, String> formControlsKey(Collection<Long> formIds) {
        return StreamUtil.toMapV(formIds, ApplicationRedisKeyUtil::formControlsKey);
    }

    /**
     * 控件
     *
     * @param controlId controlId
     * @return String
     */
    public static String controlsKey(Long controlId) {
        return ApplicationRedisConstant.CONTROLS_PREFIX + controlId;
    }

    /**
     * 控件
     *
     * @param controlIds controlIds
     * @return String
     */
    public static Map<Long, String> controlsKey(Collection<Long> controlIds) {
        return StreamUtil.toMapV(controlIds, ApplicationRedisKeyUtil::controlsKey);
    }
    // endregion

    // region 选项

    /**
     * 会话资源-控件选项
     *
     * @return String
     */
    public static String requestUuidOptionKey(Long optionId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_OPTION_PREFIX + optionId;
    }

    /**
     * 会话资源-控件选项
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidOptionKeyMap(Collection<Long> optionIds) {
        return StreamUtil.toMapKV(optionIds, e -> e, ApplicationRedisKeyUtil::requestUuidOptionKey);
    }

    /**
     * 控件选项
     *
     * @param controlId controlId
     * @return String
     */
    public static String controlOption(Long controlId) {
        return ApplicationRedisConstant.CONTROL_OPTION_PREFIX + controlId;
    }

    /**
     * 控件选项
     *
     * @param controlIds controlIds
     * @return String
     */
    public static Map<Long, String> controlOption(Collection<Long> controlIds) {
        return StreamUtil.toMapV(controlIds, ApplicationRedisKeyUtil::controlOption);
    }

    /**
     * 选项
     *
     * @param optionId optionId
     * @return String
     */
    public static String optionId(Long optionId) {
        return ApplicationRedisConstant.OPTION_PREFIX + optionId;
    }

    /**
     * 选项
     *
     * @param optionIds optionIds
     * @return String
     */
    public static Map<Long, String> optionId(Collection<Long> optionIds) {
        return StreamUtil.toMapV(optionIds, ApplicationRedisKeyUtil::optionId);
    }
    // endregion

    // region 流程

    /**
     * 会话资源-应用表单
     *
     * @return String
     */
    public static String requestUuidProcessKey(Long processId) {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.REQUEST_UUID_PROCESS_PREFIX + processId;
    }

    /**
     * 会话资源-应用流程
     *
     * @return Map
     */
    public static Map<Long, String> requestUuidProcessKeyMap(Collection<Long> processIds) {
        return StreamUtil.toMapKV(processIds, e -> e, ApplicationRedisKeyUtil::requestUuidProcessKey);
    }
    // endregion

    // region 流程
    // endregion

    // region 订单

    /**
     * 机器人创建的订单（这里用于事务补偿）
     *
     * @return String
     */
    public static String requestUuidRobotAddOrderId() {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.ROBOT_ADD_ORDER_PREFIX;
    }

    /**
     * 机器人编辑的订单（这里用于事务补偿）
     *
     * @return String
     */
    public static String requestUuidRobotModifyOrderId() {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.ROBOT_MODIFY_ORDER_PREFIX;
    }
    // endregion

    // region 机器人

    /**
     * 机器人redis队列 key
     *
     * @return String
     */
    public static String robotMqKey() {
        return ApplicationRedisKeyUtil.robotMqKey(ServletUtils.getSession().getId());
    }

    /**
     * 机器人redis队列 key
     *
     * @return String
     */
    public static String robotMqKey(String sessionId) {
        return ApplicationRedisConstant.CODE_NAME_PREFIX + ApplicationRedisConstant.ROBOT_MQ_PREFIX + sessionId;
    }

    /**
     * 机器人日志id （最新的那个。备注：该日志属于应用功能，非程序日志）
     *
     * @return String
     */
    public static String requestUuidRobotLogId() {
        String requestUuid = ServletUtils.getRequest().getHeader(AppConstant.HEADER_REQUEST_UUID);
        return RedisKeyUtil.requestUuidKey(requestUuid) + ":" + ApplicationRedisConstant.ROBOT_LOG_ID_PREFIX;
    }

    /**
     * 机器人日志详情列表
     */
    public static String robotLogKey(Long robotLogId) {
        return AppConstant.CODE_NAME_PREFIX + ApplicationRedisConstant.ROBOT_LOG_ID_PREFIX + robotLogId;
    }
    // endregion

    // region 导入模板

    /**
     * 导入模板文件-xml节点
     *
     * @return String
     */
    public static String importTemplateFileXmlNodeKey(String fileCode) {
        return ApplicationRedisConstant.IMPORT_TEMPLATE_FILE_XML_NODE_KEY + fileCode;
    }
    // endregion

}
